package com.ruoyi.system.service.impl;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.DetectionCycle;
import com.ruoyi.system.mapper.DetectionCycleMapper;
import com.ruoyi.system.service.IDetectionCycleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.ruoyi.common.utils.SecurityUtils.getLoginUser;

/**
 * 类别Service业务层处理
 *
 * @author ruoyi
 * @date 2022-02-21
 */
@Service
public class DetectionCycleServiceImpl implements IDetectionCycleService {
    @Autowired
    private DetectionCycleMapper detectionCycleMapper;

    /**
     * 查询类别
     *
     * @param id 类别主键
     * @return 类别
     */
    @Override
    public DetectionCycle selectDetectionCycleById(Long id) {
        return detectionCycleMapper.selectDetectionCycleById(id);
    }

    /**
     * 查询类别列表
     *
     * @param detectionCycle 类别
     * @return 类别
     */
    @Override
    public List<DetectionCycle> selectDetectionCycleList(DetectionCycle detectionCycle) {

        if (StringUtils.isNotNull(detectionCycle.getDeptId()) && detectionCycle.getDeptId() != 100L) {
            return detectionCycleMapper.selectCycleDeptList(detectionCycle);
        }
        return detectionCycleMapper.selectDetectionCycleList(detectionCycle);
    }


    /**
     * 查询类别列表
     *
     * @param detectionCycle 类别
     * @return 类别
     */
    @Override
    public List<DetectionCycle> listUserDeptAll(DetectionCycle detectionCycle) {
        detectionCycle.setDeptId(getLoginUser().getDeptId()); //获取当前用户登录行业id
        if (StringUtils.isNotNull(detectionCycle.getDeptId()) && detectionCycle.getDeptId() != 100L) {
            return detectionCycleMapper.selectCycleDeptList(detectionCycle);
        }
        return detectionCycleMapper.selectDetectionCycleList(detectionCycle);
    }

    /**
     * 新增类别
     *
     * @param detectionCycle 类别
     * @return 结果
     */
    @Override
    public int insertDetectionCycle(DetectionCycle detectionCycle) {
        detectionCycle.setCreateTime(DateUtils.getNowDate());
        return detectionCycleMapper.insertDetectionCycle(detectionCycle);
    }

    /**
     * 修改类别
     *
     * @param detectionCycle 类别
     * @return 结果
     */
    @Override
    public int updateDetectionCycle(DetectionCycle detectionCycle) {
        return detectionCycleMapper.updateDetectionCycle(detectionCycle);
    }

    /**
     * 批量删除类别
     *
     * @param ids 需要删除的类别主键
     * @return 结果
     */
    @Override
    public int deleteDetectionCycleByIds(Long[] ids) {
        return detectionCycleMapper.deleteDetectionCycleByIds(ids);
    }

    /**
     * 删除类别信息
     *
     * @param id 类别主键
     * @return 结果
     */
    @Override
    public int deleteDetectionCycleById(Long id) {
        return detectionCycleMapper.deleteDetectionCycleById(id);
    }


    /**
     * 校验类别名称是否唯一
     *
     * @param categoryName 类别名称
     * @return 结果
     */
    @Override
    public String checkCategoryNameUnique(String categoryName) {
        int count = detectionCycleMapper.checkCategoryNameUnique(categoryName);
        if (count > 0) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

}
